import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fse from 'fs-extra';
import fsExtra from 'fs-extra';
import path from 'path';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import { cleanTarget, makeOneHap } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { Project } from '../../plugins/project-builder/models/project-models';
import { ICaseRecord } from '../../plugins/report-builder/report-models';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption, TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  }
};

// 1. 选择product-default-entry-default进行菜单栏build-hap构建
// 在entry-build-default目录下生成一个entry-default-hap包和mini目录下生成一个entry-mini-hap包
async function buildHapDefault(project: Project, record: ICaseRecord) {
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap'
  });
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('选择product-default-entry-default进行菜单栏build-hap构建 ', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  // 在entry-build-default目录下生成一个entry-default-hap包
  const outputs = path.resolve(project.getModule('entry').moduleRootPath(), 'build', 'default', 'outputs');
  const defaultHapExists = await fse.pathExists(path.resolve(outputs, 'default', 'entry-default-unsigned.hap'));
  expect(defaultHapExists).toBe(true);
}

// 2. 选择product-default-entry-default进行菜单栏build-app构建
// 在工程-build-default目录下生成一个myapplication-default-app包且app包含有entry-build-default包
async function buildAppDefault(project: Project, record: ICaseRecord) {
  const commands = initExecuteCommand({
    mode: 'project',
    product: 'default',
    tasks: 'assembleApp'
  });
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('菜单栏build-app构建 ', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  // 在entry-build-default目录下生成一个entry-default-hap包
  const outputs = path.resolve(project.projectRootPath(), 'build', 'outputs');
  const defaultAapExists = await fse.pathExists(path.resolve(outputs, 'default', 'myapplication-default-unsigned.app'));
  expect(defaultAapExists).toBe(true);

  // 解析hap包的内容
  await fsExtra.copyFile(path.resolve(outputs, 'default', 'myapplication-default-unsigned.app'),
    path.resolve(outputs, 'default', 'myapplication-default-unsigned.zip'));
  await compressing.zip.uncompress(
    path.resolve(outputs, 'default', 'myapplication-default-unsigned.zip'),
    path.resolve(outputs, 'default', 'myapplication-default-unsigned'));

  // 查看app包是否有entry-build-default包
  const hapExists = await fse.pathExists(
    path.resolve(outputs, 'default/myapplication-default-unsigned/entry-default.hap'));
  expect(hapExists).toBe(true);
}


const testFun: TestCaseFun = async (project: Project, taskConfig: TestTaskOption, record: ICaseRecord) => {
  // 1. 选择product-default-entry-default进行make构建 ， make构建成功，在entry-build-default目录下生成一个entry-default-hap包
  await makeOneHap('entry')(project, taskConfig, record);
  await cleanTarget(project);

  // 2. 选择product-default-entry-default进行菜单栏build-hap构建
  // build-hap构建成功，在entry-build-default目录下生成一个entry-default-hap包
  await buildHapDefault(project, record);
  await cleanTarget(project);

  // 3. 选择product-default-entry-default进行菜单栏build-app构建
  await buildAppDefault(project, record);
  await cleanTarget(project);

};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Visualization_Target_FA_API10_ArkTs_Single_Module_001')
  .description('01.API10-FA单模块工程，1个product，1个target，product包含1个target')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L1).build())
  .testFun(testFun)
  .load();
